package com.hnichr.ump.api.controller;

import com.hnichr.ump.common.school.entity.SchoolEnroll;
import com.hnichr.ump.common.school.entity.Student;
import com.hnichr.ump.common.school.service.SchoolEnrollService;
import com.hnichr.ump.common.school.service.StudentService;
import com.hnichr.ump.common.school.vo.SchoolResultVO;
import com.mxpioframework.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "StudentController", description = "学生表")
@RestController
@RequestMapping("/api/student/v1")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private SchoolEnrollService enrollService;

    @PostMapping("create/student")
    @Operation(summary = "新增student", description = "新增student", method = "POST")
    public Result<SchoolResultVO> studentCardCreate(@RequestBody Student student) {
        if(StringUtils.isBlank(student.getMobile())){
            return Result.error("手机号不能为空！");
        }
        //log.info(String.valueOf("得到的值："+student));
        Student info = studentService.getByMobile(student.getMobile());
        if(info != null && StringUtils.isNotBlank(info.getAnswer()))
        {
            return Result.OK(new SchoolResultVO("03","该手机号已注册,并且已提交答案！"));
        }
        else if(info != null && StringUtils.isBlank(info.getAnswer()))
        {
            return Result.OK(new SchoolResultVO("02","该手机号已注册,未提交答案！"));
        }
        else {
            // register student info
            studentService.save(student);
            return Result.OK(new SchoolResultVO("01","成功"));
        }
    }

   /* @GetMapping("find/student/{mobile}")
    @Operation(summary = "查找学生", description = "查找学生", method = "GET")
    public Result<Student> findStudentCard(@PathVariable(name = "mobile", required = true) String mobile) {
        log.info("查询的手机号为："+mobile);
        Student stu = studentService.getByMobile(mobile);
        if(stu == null){
            return Result.error("未查询到人员数据！");
        }
        return Result.OK(stu);
    }*/

    @PutMapping("commit/answer")
    @Operation(summary = "提交答案", description = "提交答案", method = "PUT")
    public Result<SchoolResultVO> updateStudentCard(@RequestBody Student student) {
        if(StringUtils.isBlank(student.getMobile())){
            return Result.error("手机号不能为空！");
        }
        Student card = studentService.getByMobile(student.getMobile());
        if(card == null) {
            return Result.OK(new SchoolResultVO("01","请您先注册再提交答案！"));
        }
        if(StringUtils.isNotBlank(card.getAnswer())){
            return Result.OK(new SchoolResultVO("03","已提交答案！"));
        }
        card.setAnswer(student.getAnswer());
        studentService.update(card);
        return Result.OK(new SchoolResultVO("02","成功！"));
    }

    @PostMapping("create/enroll")
    @Operation(summary = " 报名", description = "报名", method = "POST")
    public Result<SchoolResultVO> schoolEnroll(@RequestBody SchoolEnroll enroll) {
        if(StringUtils.isBlank(enroll.getMobile())){
            return Result.error("手机号不能为空！");
        }
        if(StringUtils.isBlank(enroll.getName())){
            return Result.error("姓名不能为空！");
        }
        if(StringUtils.isBlank(enroll.getSchool())){
            return Result.error("学校不能为空！");
        }
       /* if(StringUtils.isBlank(enroll.getMajor())){
            return Result.error("专业不能为空！");
        }*/
        enrollService.save(enroll);
        return Result.OK(new SchoolResultVO("01","成功"));
    }
}
